package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysOutlets;
import com.ruoyi.system.service.ISysOutletsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务网点Controller
 * 
 * @author ruoyi
 * @date 2021-05-26
 */
@Api(tags = "服务网点模块")
@RestController
@RequestMapping("/system/outlets")
public class SysOutletsController extends BaseController
{
    @Autowired
    private ISysOutletsService sysOutletsService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询服务网点列表
     */
    @ApiOperation("查询服务网点列表")
//    @PreAuthorize("@ss.hasPermi('system:outlets:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOutlets sysOutlets)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        sysOutlets.setDeptId(null == sysOutlets.getDeptId() ? user.getDeptId() : sysOutlets.getDeptId());
        List<SysOutlets> list = sysOutletsService.selectSysOutletsList(sysOutlets);
        return getDataTable(list);
    }

    /**
     * 导出服务网点列表
     */
//    @PreAuthorize("@ss.hasPermi('system:outlets:export')")
    @Log(title = "服务网点", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysOutlets sysOutlets)
    {
        List<SysOutlets> list = sysOutletsService.selectSysOutletsList(sysOutlets);
        ExcelUtil<SysOutlets> util = new ExcelUtil<SysOutlets>(SysOutlets.class);
        return util.exportExcel(list, "服务网点数据");
    }

    /**
     * 获取服务网点详细信息
     */
    @ApiOperation("获取服务网点详细信息")
//    @PreAuthorize("@ss.hasPermi('system:outlets:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysOutletsService.selectSysOutletsById(id));
    }

    /**
     * 新增服务网点
     */
    @ApiOperation("新增服务网点")
//    @PreAuthorize("@ss.hasPermi('system:outlets:add')")
    @Log(title = "服务网点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOutlets sysOutlets)
    {
        return toAjax(sysOutletsService.insertSysOutlets(sysOutlets));
    }

    /**
     * 修改服务网点
     */
    @ApiOperation("修改服务网点")
//    @PreAuthorize("@ss.hasPermi('system:outlets:edit')")
    @Log(title = "服务网点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOutlets sysOutlets)
    {
        return toAjax(sysOutletsService.updateSysOutlets(sysOutlets));
    }

    /**
     * 删除服务网点
     */
    @ApiOperation("删除服务网点")
//    @PreAuthorize("@ss.hasPermi('system:outlets:remove')")
    @Log(title = "服务网点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysOutletsService.deleteSysOutletsByIds(ids));
    }

    /**
     * 获取网点SN
     */
    @GetMapping("/sn")
    public Object getSN() {
        Integer n;
        String sn = null;
        if (redisCache.hasKey(Constants.OUTLETS_SN)) {
            n = Integer.parseInt(redisCache.getCacheObject(Constants.OUTLETS_SN)) + 1;
        } else {
            String lastSN = sysOutletsService.getSN();
            if (null == lastSN || ObjectUtils.isEmpty(lastSN)) {
                sn = "N" + "00000001";
                return AjaxResult.success(sn);
            }
            n = Integer.parseInt(lastSN.substring(lastSN.length() - 8)) + 1;
        }
        if (n >= 10000000) {
            sn = "N" + n;
        } else if (n >= 1000000) {
            sn = "N" + "0" + n;
        } else if (n >= 100000) {
            sn = "N" + "00" + n;
        } else if (n >= 10000) {
            sn = "N" + "000" + n;
        } else if (n >= 1000) {
            sn = "N" + "0000" + n;
        } else if (n >= 100) {
            sn = "N" + "00000" + n;
        } else if (n >= 10) {
            sn = "N" + "000000" + n;
        } else if (n >= 1) {
            sn = "N" + "0000000" + n;
        }
        redisCache.setCacheObject(Constants.OUTLETS_SN, String.valueOf(n));
        return AjaxResult.success(sn);
    }
}
